import utils from '@/utils/utils.js';

export const request = (options) => {
	let baseUrl = 'https://www.gxjuxinxiang.com/api';//https://sport.dbs12580.com:441/api   https://www.gxjuxinxiang.com/api
	const {
		type = 'GET', // 默认为GET请求
			url,
			data = {},
			header = {},
			intercept = true, // 是否截取错误进行提示
	} = options;
	header.Authorization = 'Basic c2FiZXI6c2FiZXJfc2VjcmV0';
	header['Tenant-Id'] = '000000';
	const token = uni.getStorageSync("token");
	const tokenType = uni.getStorageSync("tokenType");
	if (token) {
		header['Blade-Auth'] = `${tokenType} ${token}`;
	}

	return new Promise((resolve, reject) => {
		return uni.request({
			url: baseUrl + url,
			method: type,
			data: data,
			dataType: 'JSON',
			header: header,
			timeout: 40000,
			success: res => {
				const result = res.data;//JSON.parse(res.data); 如果为小程序需要使用到parse
				if (res.statusCode === 200) {
					if (intercept) {
						if (result.code === 200) {
							return resolve(result);
						} else {
							uni.hideLoading();
							uni.stopPullDownRefresh();
							uni.showModal({
								title: '提示',
								content: result.msg,
								showCancel: false
							});
							return resolve(result);
						}
					} else {
						return resolve(result);
					}
				} else if (res.statusCode === 401) {
					uni.hideLoading();
					utils.onLogout();
				} else {
					uni.hideLoading();
					uni.stopPullDownRefresh();
					uni.showToast({
						icon: 'error',
						title: result.msg || result.error_description || '网络错误，请稍后重试',
						mask: true,
						duration: 2000
					});
					reject();
				}
			},
			fail: error => {
				uni.hideLoading();
				uni.showToast({
					icon: 'error',
					title: '网络信号差',
					mask: true,
					duration: 2000
				});
				return reject(error);
			}
		})
	})
}

export default request;




